package com.base.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.base.common.core.page.TableDataInfo;
import com.base.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.base.system.domain.bo.TSysRoleBo;
import com.base.system.domain.vo.TSysRoleVo;
import com.base.system.mapper.TSysRoleMapper;
import com.base.system.service.ITSysRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.base.common.utils.StringUtils;
import com.base.system.domain.TSysRole;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 角色Service业务层处理
 *
 * @author base
 * @date 2023-10-18
 */
@RequiredArgsConstructor
@Service
public class TSysRoleServiceImpl implements ITSysRoleService {

    private final TSysRoleMapper baseMapper;

    /**
     * 查询角色
     */
    @Override
    public TSysRoleVo queryById(String roleId){
        return baseMapper.selectVoById(roleId);
    }

    /**
     * 查询角色列表
     */
    @Override
    public TableDataInfo<TSysRoleVo> queryPageList(TSysRoleBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TSysRole> lqw = buildQueryWrapper(bo);
        Page<TSysRoleVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询角色列表
     */
    @Override
    public List<TSysRoleVo> queryList(TSysRoleBo bo) {
        LambdaQueryWrapper<TSysRole> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TSysRole> buildQueryWrapper(TSysRoleBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TSysRole> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getRoleName()), TSysRole::getRoleName, bo.getRoleName());
        lqw.like(StringUtils.isNotBlank(bo.getEnName()), TSysRole::getEnName, bo.getEnName());
        lqw.eq(StringUtils.isNotBlank(bo.getRoleType()), TSysRole::getRoleType, bo.getRoleType());
        lqw.eq(StringUtils.isNotBlank(bo.getOperator()), TSysRole::getOperator, bo.getOperator());
        return lqw;
    }

    /**
     * 新增角色
     */
    @Override
    public Boolean insertByBo(TSysRoleBo bo) {
        TSysRole add = BeanUtil.toBean(bo, TSysRole.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setRoleId(add.getRoleId());
        }
        return flag;
    }

    /**
     * 修改角色
     */
    @Override
    public Boolean updateByBo(TSysRoleBo bo) {
        TSysRole update = BeanUtil.toBean(bo, TSysRole.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TSysRole entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除角色
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
